package com.irdstudio.tdp.console.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.irdstudio.tdp.console.dao.BatTaskNlsjobConfDao;
import com.irdstudio.tdp.console.dao.domain.BatTaskNlsjobConf;
import com.irdstudio.tdp.console.service.facade.BatTaskNlsjobConfService;
import com.irdstudio.tdp.console.service.vo.BatTaskNlsjobConfVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.sdk.beans.core.base.FrameworkService;

/**
 * <p>
 * 任务运行作业配置的Service实现类
 * </p>
 * @author szbd
 * @createDate 2020-06-23 09:03:35
 */
@Service("taskNlsjobConfService")
public class BatTaskNlsjobConfServiceImpl implements BatTaskNlsjobConfService, FrameworkService {
    
    private static Logger logger = LoggerFactory.getLogger(BatTaskNlsjobConfServiceImpl.class);
    
    @Autowired
    private BatTaskNlsjobConfDao batTaskNlsjobConfDao;
    
    @Override
    public int insertBatTaskNlsjobConf(BatTaskNlsjobConfVO inBatTaskNlsjobConfVo) {
        logger.info("新增任务运行作业配置数据的参数为：" + JSONObject.toJSONString(inBatTaskNlsjobConfVo));
        if(Objects.isNull(inBatTaskNlsjobConfVo)){
            return 0;
        }
        int count = 0;
        try {
            BatTaskNlsjobConf batTaskNlsjobConf = new BatTaskNlsjobConf();
            beanCopy(inBatTaskNlsjobConfVo, batTaskNlsjobConf);
            
            //处理排序
            String taskId = batTaskNlsjobConf.getTaskId();
            int maxSort = sortBatTaskNlsjobConf(taskId);
            
            batTaskNlsjobConf.setConfSort(maxSort+1);
            count = batTaskNlsjobConfDao.insertBatTaskNlsjobConf(batTaskNlsjobConf);
        } catch (Exception e) {
            logger.error("新增任务运行作业配置数据发生异常！", e);
            count = -1;
        }
        logger.info("当前新增任务运行作业配置数据条数为：" + count);
        return count;
    }
    
    @Override
    public int deleteBatTaskNlsjobConfByPk(BatTaskNlsjobConfVO inBatTaskNlsjobConfVo) {
        logger.info("删除任务运行作业配置数据的参数为：" + JSONObject.toJSONString(inBatTaskNlsjobConfVo));
        if(Objects.isNull(inBatTaskNlsjobConfVo)){
            return 0;
        }
        int count = 0;
        try {
            BatTaskNlsjobConf batTaskNlsjobConf = new BatTaskNlsjobConf();
            beanCopy(inBatTaskNlsjobConfVo, batTaskNlsjobConf);
            count = batTaskNlsjobConfDao.deleteBatTaskNlsjobConfByPk(batTaskNlsjobConf);
            sortBatTaskNlsjobConf(batTaskNlsjobConf.getTaskId());
        } catch (Exception e) {
            logger.error("删除任务运行作业配置数据发生异常！", e);
            count = -1;
        }
        logger.info("删除的任务运行作业配置数据条数为：" + count);
        return count;
    }
    
    @Override
    public int updateBatTaskNlsjobConfByPk(BatTaskNlsjobConfVO inBatTaskNlsjobConfVo) {
        logger.info("修改任务运行作业配置数据的参数为：" + JSONObject.toJSONString(inBatTaskNlsjobConfVo));
        if(Objects.isNull(inBatTaskNlsjobConfVo)){
            return 0;
        }
        int count = 0;
        try {
            BatTaskNlsjobConf batTaskNlsjobConf = new BatTaskNlsjobConf();
            beanCopy(inBatTaskNlsjobConfVo, batTaskNlsjobConf);
            count = batTaskNlsjobConfDao.updateBatTaskNlsjobConfByPk(batTaskNlsjobConf);
        } catch (Exception e) {
            logger.error("修改数据发生异常！", e);
            count = -1;
        }
        logger.info("修改的任务运行作业配置数据条数为" + count);
        return count;
    }
    
    @Override
    public BatTaskNlsjobConfVO queryBatTaskNlsjobConfByPk(BatTaskNlsjobConfVO inBatTaskNlsjobConfVo) {
        logger.info("根据主键查询任务运行作业配置的请求参数为：" + JSONObject.toJSONString(inBatTaskNlsjobConfVo));
        if(Objects.isNull(inBatTaskNlsjobConfVo)){
            return null;
        }
        try {
            BatTaskNlsjobConf batTaskNlsjobConf = new BatTaskNlsjobConf();
            beanCopy(inBatTaskNlsjobConfVo, batTaskNlsjobConf);
            batTaskNlsjobConf = batTaskNlsjobConfDao.queryBatTaskNlsjobConfByPk(batTaskNlsjobConf);
            if (Objects.nonNull(batTaskNlsjobConf)) {
                BatTaskNlsjobConfVO outBatTaskNlsjobConfVo = beanCopy(batTaskNlsjobConf, new BatTaskNlsjobConfVO());
                return outBatTaskNlsjobConfVo;
            } else {
                logger.info("根据主键查询任务运行作业配置的记录为空！");
            }
        } catch (Exception e) {
            logger.error("根据主键查询任务运行作业配置记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public BatTaskNlsjobConfVO querySingleBatTaskNlsjobConfByCondition(BatTaskNlsjobConfVO inBatTaskNlsjobConfVo) {
        logger.info("根据条件查询单笔任务运行作业配置的请求参数为：" + JSONObject.toJSONString(inBatTaskNlsjobConfVo));
        if(Objects.isNull(inBatTaskNlsjobConfVo)){
            return null;
        }
        try {
            BatTaskNlsjobConf batTaskNlsjobConf = new BatTaskNlsjobConf();
            beanCopy(inBatTaskNlsjobConfVo, batTaskNlsjobConf);
            batTaskNlsjobConf = batTaskNlsjobConfDao.querySingleBatTaskNlsjobConfByCondition(batTaskNlsjobConf);
            if (Objects.nonNull(batTaskNlsjobConf)) {
                BatTaskNlsjobConfVO outBatTaskNlsjobConfVo = beanCopy(batTaskNlsjobConf, new BatTaskNlsjobConfVO());
                return outBatTaskNlsjobConfVo;
            } else {
                logger.info("根据条件查询单笔任务运行作业配置的记录为空！");
            }
        } catch (Exception e) {
            logger.error("根据条件查询单笔任务运行作业配置记录发生异常！", e);
        }
        return null;
    }
    
    @Override
    public List<BatTaskNlsjobConfVO> queryBatTaskNlsjobConfByCondition(BatTaskNlsjobConfVO inBatTaskNlsjobConfVo) {
        logger.info("条件查询任务运行作业配置的请求参数为：" + JSONObject.toJSONString(inBatTaskNlsjobConfVo));
        if(Objects.isNull(inBatTaskNlsjobConfVo)){
            return null;
        }
        try {
            List<BatTaskNlsjobConf> dataList = batTaskNlsjobConfDao.queryBatTaskNlsjobConfByCondition(inBatTaskNlsjobConfVo);
            if (Objects.nonNull(dataList)) {
                List<BatTaskNlsjobConfVO> resultList = new ArrayList<BatTaskNlsjobConfVO>();
                resultList = (List<BatTaskNlsjobConfVO>) beansCopy(dataList, BatTaskNlsjobConfVO.class);
                return resultList;
            } else {
                logger.info("条件查询任务运行作业配置的数据为空！");
            }
        } catch (Exception e) {
            logger.error("条件查询任务运行作业配置发生异常！", e);
        }
        return null;
    }
    
    @Override
    public List<BatTaskNlsjobConfVO> queryBatTaskNlsjobConfByConditionByPage(BatTaskNlsjobConfVO inBatTaskNlsjobConfVo) {
        logger.info("条件分页查询任务运行作业配置的请求参数为：" + JSONObject.toJSONString(inBatTaskNlsjobConfVo));
        if(Objects.isNull(inBatTaskNlsjobConfVo)){
            return null;
        }
        try {
            List<BatTaskNlsjobConf> dataList = batTaskNlsjobConfDao.queryBatTaskNlsjobConfByConditionByPage(inBatTaskNlsjobConfVo);
            if (Objects.nonNull(dataList)) {
                List<BatTaskNlsjobConfVO> resultList = new ArrayList<BatTaskNlsjobConfVO>();
                pageSet(resultList, inBatTaskNlsjobConfVo);
                resultList = (List<BatTaskNlsjobConfVO>) beansCopy(dataList, BatTaskNlsjobConfVO.class);
                return resultList;
            } else {
                logger.info("条件分页查询任务运行作业配置的数据为空！");
            }
        } catch (Exception e) {
            logger.error("条件分页查询任务运行作业配置发生异常！", e);
        }
        return null;
    }
    
    /**
     * 排序
     * @param taskId
     * @return
     */
    private int sortBatTaskNlsjobConf(String taskId) {
        List<BatTaskNlsjobConf> dataList = batTaskNlsjobConfDao.queryBatTaskNlsjobConfByTaskIdOrderBySort(taskId);
        int maxSort = 0;
        if(Objects.nonNull(dataList) && dataList.size()>0) {
            maxSort = dataList.size();
            BatTaskNlsjobConf conf = null;
            BatTaskNlsjobConf updateBean = new BatTaskNlsjobConf();
            updateBean.setTaskId(taskId);
            for(int i=0; i<dataList.size(); i++) {
                conf = dataList.get(i);
                updateBean.setJobName(conf.getJobName());
                updateBean.setConfSort(i+1);
                batTaskNlsjobConfDao.updateBatTaskNlsjobConfByPk(updateBean);
            }
        }
        return maxSort;
    }

    @Override
    public int moveUpBatTaskNlsjobConf(BatTaskNlsjobConfVO batTaskNlsjobConfVo) {
        String taskId = batTaskNlsjobConfVo.getTaskId();
        int sort = batTaskNlsjobConfVo.getConfSort();
        BatTaskNlsjobConf confFromDB = batTaskNlsjobConfDao.queryBatTaskNlsjobConf4Move(taskId, sort, "up");
        if(Objects.nonNull(confFromDB)) {
            BatTaskNlsjobConf conf = new BatTaskNlsjobConf();
            conf.setTaskId(taskId);
            conf.setJobName(batTaskNlsjobConfVo.getJobName());
            conf.setConfSort(confFromDB.getConfSort());
            batTaskNlsjobConfDao.updateBatTaskNlsjobConfByPk(conf);
            
            confFromDB.setConfSort(sort);
            batTaskNlsjobConfDao.updateBatTaskNlsjobConfByPk(confFromDB);
        }
        return 1;
    }

    @Override
    public int moveDownBatTaskNlsjobConf(BatTaskNlsjobConfVO batTaskNlsjobConfVo) {
        String taskId = batTaskNlsjobConfVo.getTaskId();
        int sort = batTaskNlsjobConfVo.getConfSort();
        BatTaskNlsjobConf confFromDB = batTaskNlsjobConfDao.queryBatTaskNlsjobConf4Move(taskId, sort, "down");
        if(Objects.nonNull(confFromDB)) {
            BatTaskNlsjobConf conf = new BatTaskNlsjobConf();
            conf.setTaskId(taskId);
            conf.setJobName(batTaskNlsjobConfVo.getJobName());
            conf.setConfSort(confFromDB.getConfSort());
            batTaskNlsjobConfDao.updateBatTaskNlsjobConfByPk(conf);
            
            confFromDB.setConfSort(sort);
            batTaskNlsjobConfDao.updateBatTaskNlsjobConfByPk(confFromDB);
        }
        return 1;
    }
}